﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microhard.Core.DBUtility;
using Microhard.FMCG.Buz.Interfaces;
using Microhard.FMCG.Buz.Impls;
using Microhard.FMCG.Buz.Models;
using Microhard.Core.Mail;
using System.Configuration;
using Microhard.Core;

namespace Microhard.FMCGClient
{
    public partial class frmMain : Form
    {
        /// <summary>
        /// 使用线程安全的方式操作控件
        /// </summary>
        /// <param name="text"></param>
        private delegate void SetNotifiUICallback(NotifiUIModel model);

        public frmMain()
        {
            InitializeComponent();
        }

        #region Public Mentod
        public void ToNotifiUI(NotifiUIModel model)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.lbRunningInfo.InvokeRequired)
            {
                SetNotifiUICallback d = new SetNotifiUICallback(ToNotifiUI);
                this.Invoke(d, new object[] { model });
            }
            else
            {
                //在运行情况中显示
                this.lbRunningInfo.Items.Add(model.text);
                lbRunningInfo.SelectedIndex = lbRunningInfo.Items.Count - 1;
                lbRunningInfo.SelectedIndex = -1;

                //显示运行状态
                if (model.KASystem != null && string.IsNullOrEmpty(model.KASystem.AllRunningDescription) == false)
                {
                    this.groupBox1.Text = model.KASystem.AllRunningDescription;
                }

                //界面提醒
                switch (model.NotifiType)
                {
                    case FormNotifiType.News:
                        frmListNews frm = new frmListNews();
                        frm.MdiParent = this;
                        frm.Activate();
                        frm.ShowNew();
                        frm.Show();
                        break;
                    case FormNotifiType.Order:
                        frmListOrder frmOrder = new frmListOrder();
                        frmOrder.MdiParent = this;
                        frmOrder.Activate();
                        frmOrder.ShowNew();
                        frmOrder.Show();
                        break;
                }
            }
        }

        public void EnableMenu()
        {
            tsMenuItem.Enabled = true;
        }
        #endregion

        #region Event

        private void loginMenuItem_Click(object sender, EventArgs e)
        {
            frmLogin login = new frmLogin();
            login.MdiParent = this;
            login.Show();
            return;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            RunningMonitor.Monitor.OnGetNewNews += new GetNewNews(Monitor_OnGetNewNews);
            RunningMonitor.Monitor.OnGetNewOrder += new GetNewOrder(Monitor_OnGetNewOrder);
            RunningMonitor.Monitor.OnKASystemError += new OnKASystemError(Monitor_OnKASystemError);
            RunningMonitor.Monitor.OnKASystemNotice += new OnSystemNotice(Monitor_OnKASystemNotice);
            RunningMonitor.Monitor.OnNeedReLogin += new NeedReLogin(Monitor_OnNeedReLogin);
            RunningMonitor.Monitor.OnCommonRunning += new GetRunning(Monitor_OnCommonRunning);
            RunningMonitor.Monitor.OnLogin += new GetLoginInfo(Monitor_OnLogin);
            //RunningMonitor.Monitor.OnStatements += new GetNewStatements(Monitor_OnStatements);
            //RunningMonitor.Monitor.OnGetRecGoodInfoError += new GetOrderRecInfoError(Monitor_OnGetRecGoodInfoError);
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Log.Info("frmMain_FormClosing", DateTime.Now.ToString());
            RunningMonitor.Monitor.Stop();
        }

        void Monitor_OnGetRecGoodInfoError(KASystemRunningModel info)
        {
            string message = "更新收货单失败";
            //界面提醒
            this.ToNotifiUI(new NotifiUIModel { text = message, KASystem = info });
        }

        void Monitor_OnStatements(KASystemRunningModel info)
        {
            string message = "累计销售额:" + info.AllStatementsAmount;
            //界面提醒
            this.ToNotifiUI(new NotifiUIModel { text = message, KASystem = info });
        }

        void Monitor_OnLogin(KASystemRunningModel info)
        {
            string message = info.System + info.SystemNotice;
            //界面提醒
            this.ToNotifiUI(new NotifiUIModel { text = message, KASystem = info });
        }

        void Monitor_OnCommonRunning(KASystemRunningModel info)
        {
            string message = info.System + info.CommonInfo;
            //界面提醒
            this.ToNotifiUI(new NotifiUIModel { text = message, KASystem = info });
        }


        void Monitor_OnNeedReLogin(KASystemRunningModel info)
        {
            string message = string.Format(ResMessage.ReLoginError, info.System);
            //界面提醒
            this.ToNotifiUI(new NotifiUIModel { text = message, KASystem = info });
        }

        void Monitor_OnKASystemNotice(KASystemRunningModel info)
        {
            string message = string.Format(ResMessage.SystemNotice, info.System);
            //界面提醒
            this.ToNotifiUI(new NotifiUIModel { text = message, KASystem = info, news = new NewsModel { Title = message, Detail = info.SystemNotice }, NotifiType = FormNotifiType.News });
        }

        void Monitor_OnKASystemError(KASystemRunningModel info)
        {
            string message = info.System + info.SystemNotice;
            //界面提醒
            this.ToNotifiUI(new NotifiUIModel { text = message, KASystem = info });
        }

        void Monitor_OnGetNewOrder(KASystemRunningModel info)
        {
            StringBuilder orderNo = new StringBuilder();
            //界面提醒
            info.NewOrder.ForEach(i =>
            {
                string message = string.Format(ResMessage.NewOrder, info.System, i.Code);
                this.ToNotifiUI(new NotifiUIModel { text = message, order = i, NotifiType = FormNotifiType.Order, KASystem = info });
            });
        }

        void Monitor_OnGetNewNews(KASystemRunningModel info)
        {
            //界面提醒
            info.TopNews.ForEach(i =>
            {
                string message = string.Format(ResMessage.TopNews, info.System, i.Title);
                this.ToNotifiUI(new NotifiUIModel { text = message, news = i, NotifiType = FormNotifiType.News, KASystem = info });
            });
        }
        #endregion

        private void OrderTSMI_Click(object sender, EventArgs e)
        {
            frmListOrder frmOrder = new frmListOrder();
            frmOrder.MdiParent = this;
            frmOrder.Show();
        }

        private void NewsTSMI_Click(object sender, EventArgs e)
        {
            frmListNews frmNews = new frmListNews();
            frmNews.MdiParent = this;
            frmNews.Show();
        }
    }
}
